package com.kinghunt.misp.web.controller.userx;

import com.kinghunt.misp.common.constant.SystemConstant;
import com.kinghunt.misp.common.dto.userx.RoleDTO;
import com.kinghunt.misp.common.param.userx.RoleParam;
import com.kinghunt.misp.common.param.userx.RoleQueryCondition;
import com.kinghunt.misp.service.api.userx.RoleReadService;
import com.kinghunt.misp.service.api.userx.RoleWriteService;
import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * 角色管理
 *
 * @Author jiangtao
 * @Date 2023/7/4 18:46
 */
@RestController
@RequestMapping("/api/userx/role/")
public class RoleController {
    @Autowired
    RoleReadService roleReadService;
    @Autowired
    RoleWriteService roleWriteService;

    /**
     * 根据ID查询角色信息
     *
     * @param id 角色ID
     * @return 角色信息
     */
    @GetMapping("getRoleInfoById")
    public ApiResult<RoleDTO> getRoleInfoById(Long id) {
        return roleReadService.getRoleById(id);
    }

    /**
     * 搜索角色分页
     *
     * @param condition 分页查询条件
     * @return 分页结果
     */
    @GetMapping("pageRoleInfos")
    public ApiResult<PageInfo<RoleDTO>> pageRoleInfos(RoleQueryCondition condition) {
        return roleReadService.pageRoleInfos(condition);
    }

    /**
     * 新增角色
     *
     * @param roleParam 新增角色信息
     * @return 新增角色ID
     */
    @PostMapping(value = "addRole", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult<Long> addRole(@RequestBody RoleParam roleParam) {
        if (StringUtils.isBlank(roleParam.getAppCode())) {
            roleParam.setAppCode(SystemConstant.SYSTEM_CODE);
        }
        return roleWriteService.addRole(roleParam);
    }

    /**
     * 更新角色
     *
     * @param roleParam 更新角色信息
     * @return 是否成功
     */
    @PostMapping(value = "updateRole", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult<Boolean> updateRole(@RequestBody RoleParam roleParam) {
        return roleWriteService.updateRoleInfo(roleParam);
    }

    /**
     * 删除角色
     *
     * @param id 角色ID
     * @return 是否成功
     */
    @GetMapping("deleteRole")
    public ApiResult<Boolean> deleteRole(Long id) {
        return roleWriteService.deleteRole(id);
    }
}
